
using System;
using System.Collections.Generic;

namespace MxitAPI {
	
	/**
	 * The message sent to log a user into the MXit servers
	 */
	public class LoginProtocolMessage : ProtocolMessage {
		
		// -------------------------------------
		// Request variables
		// -------------------------------------
		[ RequestData ]
		public string Password { get; set; }
		
		[ RequestData ]
		public string Version { get; set; }
		
		// Todo Make GetContacts an enum
		[ RequestData ]
		public int GetContacts { get; set; }
		
		[ RequestData ]
		public string Capabilities { get; set; }
		
		[ RequestData ]
		public string DistributionCode { get; set; }
		
		[ RequestData ]
		public int Features { get; set; }
		
		[ RequestData ]
		public string Dialingcode { get; set; }
		
		[ RequestData ]
		public string Locale { get; set; }
		
		// Optional request variables
		[ RequestData ]
		public string[] SplashName { get; set; }
		
		
		// -------------------------------------
		// Response variables
		// -------------------------------------
		//[ ResponseData ]
		public int SessionId { get; set; }
		
		[ ResponseData ]
		public string Depricated { get; set; }
		
		[ ResponseData ]
		public string ReturnedLoginName { get; set; }
		
		[ ResponseData ]
		public int Date { get; set; }
		
		[ ResponseData ]
		public string Url { get; set; }
		
		[ ResponseData ]
		public int MaxSupportedVersion { get; set; }
		
		// Fixme why is the priceplan not working for http?
		//[ ResponseData ]
		public int PricePlan { get; set; }
		
		[ ResponseData ]
		public string Flags { get; set; }
		
		//[ ResponseData ]
		public string[][] PollData { get; set; }
		
		/*
		 * Set the commandNum to 1
		 */
		public LoginProtocolMessage() : base( 1 ) {
		}
		
		/**
		 * Overrides the base class method so that the sessionId can be set and the array resized
		 */
		public override void SetResponse( string[][] response ) {
			
			string[][] newResponse;
				
			// If the login was successful get the session Id
			if ( Int32.Parse( response[1][0] ) == ( int ) ErrorCode.NoError ) {			
				// Set the sessionId
				SessionId = Int32.Parse( response[2][0] );
					
				// Remove the rows that contain the SessionId and pollData
				newResponse = Utilities.RemoveArrayRow( response, 2 );
				
				// It seems that the price plan is not returned for http logins.
				// Populate price plan field manually if it exists.
				if ( newResponse[2].Length == 7 ){
					PricePlan = Int32.Parse( newResponse[2][5] );
					newResponse[2] = Utilities.RemoveArrayRow( newResponse[2], 5 );
				}
			}
			else {
				newResponse = response;
			}
			
			// Populate the rest of the fields for LoginMessage
			base.SetResponse( newResponse );
		}
		
	}
}
